package com.cpi.basilos.location.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import com.cpi.basilos.location.dto.LocationDTO;
import com.cpi.basilos.location.exception.LocationException;
import com.cs.common.exception.CSException;
import com.cs.common.pool.DBDAO;

public class LocationDAO extends DBDAO {
	private static org.apache.log4j.Logger logger = Logger.getLogger(LocationDAO.class);
	
	private static final String SQL_GET_LOCATION = "select IDTIPOGRANO, EXISTENCIA, CAPACIDAD from VW_UBICACION where idalmacen = ? and idUbicacion = ?";//$NON-NLS-1$
	public LocationDTO getLocation(int warehouseId, String locationId)throws LocationException{
		LocationDTO locationDTO = null;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try{
			preparedStatement = prepareSQL(SQL_GET_LOCATION, dsBasilos);
			preparedStatement.setInt(1, warehouseId);
			preparedStatement.setString(2, locationId);
			resultSet = executeQuery(preparedStatement);
			if(resultSet.next()){
				locationDTO = new LocationDTO();
				locationDTO.setWarehouseId(warehouseId);
				locationDTO.setLocationId(locationId);
				locationDTO.setCornTypeId(resultSet.getInt(1));
				locationDTO.setStock(resultSet.getDouble(2));
				locationDTO.setCapacity(resultSet.getDouble(3));
			}
			connection = preparedStatement.getConnection();
		}catch(Exception exception){
			LocationException locationException = null;
			exception.printStackTrace(System.out);
			locationException = new LocationException(exception, CSException.LAYER_DAO, CSException.ACTION_LISTS);
			logger.error(exception);
			throw locationException;
		}finally{
			try{
				closeConnection(connection);
			}catch(Exception exception){}
		}
		return locationDTO;
	}
}